/**
  ******************************************************************************
  * @file    r1_vl1_pwm_curr_fdbk.h
  * @author  Motor Control SDK Team, ST Microelectronics
  * @brief   This file contains all definitions and functions prototypes for the
  *          r1_vl1_pwm_curr_fdbk component of the Motor Control SDK.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  * @ingroup r1_vl1_pwm_curr_fdbk
  */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __R1_VL1_PWM_CURR_FDBK_H
#define __R1_VL1_PWM_CURR_FDBK_H

#ifdef __cplusplus
 extern "C" {
#endif /* __cplusplus */

/* Includes ------------------------------------------------------------------*/
#include "pwm_curr_fdbk.h"

/** @addtogroup MCSDK
  * @{
  */

/** @addtogroup pwm_curr_fdbk
  * @{
  */

/** @addtogroup r1_vl1_pwm_curr_fdbk
  * @{
  */

/* Exported constants --------------------------------------------------------*/
#define GPIO_NoRemap_TIM1 ((uint32_t)(0))

/** @{ */
#define EOFOC 0x0001u /**< Flag to indicate end of FOC duty available */
#define STBD3 0x0002u /**< Flag to indicate which phase has been distorted
                           in boundary 3 zone (A or B)*/
#define DSTEN 0x0004u /**< Flag to indicate if the distortion must be performed
                           or not (in case of charge of bootstrap capacitor phase
                           is not required)*/
#define SOFOC 0x0008u /**< This flag will be reset to zero at the begin of FOC
                           and will be set in the UP IRQ. If at the end of
                           FOC it is set the software error must be generated*/
#define CALIB 0x0010u /**< This flag is used to indicate the ADC calibration
                           phase in order to avoid concurrent regular conversions*/
/** @} */

/* Exported types ------------------------------------------------------------*/
/**
 * @brief PWMC_R1_VL1 component parameters structure
 *
 * @attention This structure shall be removed at cubification time.
 *
 */
typedef struct
{

   /* Current reading A/D Conversions initialization --------------------------*/
   uint8_t hIChannel;              /*!< ADC channel used for conversion of
                                        current. It must be equal to
                                        ADC_CHANNEL_x x= 0, ..., 15*/
  ADC_TypeDef* ADCx_Inj;          /*!< ADC Peripheral used for phase current sampling */

 /* PWM generation parameters --------------------------------------------------*/
   TIM_TypeDef* TIMx;              /*!< Timer used for PWM generation. It should be
                                        TIM1 */
   TIM_TypeDef* TIMx_2;            /*!< Auxiliary timer used for single shunt */

   uint16_t hDeadTime;             /*!< Dead time in number of TIM clock
                                        cycles. If CHxN are enabled, it must
                                        contain the dead time to be generated
                                        by the microcontroller, otherwise it
                                        expresses the maximum dead time
                                        generated by driving network*/
   uint8_t  RepetitionCounter;    /*!< It expresses the number of PWM
                                        periods to be elapsed before compare
                                        registers are updated again. In
                                        particular:
                                        RepetitionCounter= (2* PWM periods) -1*/
   uint16_t hTafter;               /*!< It is the sum of dead time plus rise time
                                        express in number of TIM clocks.*/
   uint16_t hTbefore;              /*!< It is the value of sampling time
                                        expressed in numbers of TIM clocks.*/
   uint16_t hTMin;                 /*!< It is the sum of dead time plus rise time
                                        plus sampling time express in numbers of
                                        TIM clocks.*/
   uint16_t hHTMin;                /*!< It is the half of hTMin value.*/
   uint16_t hTSample;              /*!< It is the sampling time express in
                                        numbers of TIM clocks.*/
   uint16_t hMaxTrTs;              /*!< It is the maximum between twice of rise
                                        time express in number of TIM clocks and
                                        twice of sampling time express in numbers
                                        of TIM clocks.*/

 /* PWM Driving signals initialization ----------------------------------------*/

   LowSideOutputsFunction_t LowSideOutputs; /*!< Low side or enabling signals
                                                 generation method are defined
                                                 here.*/

   GPIO_TypeDef * pwm_en_u_port;
   uint32_t      pwm_en_u_pin;
   GPIO_TypeDef * pwm_en_v_port;
   uint32_t      pwm_en_v_pin;
   GPIO_TypeDef * pwm_en_w_port;
   uint32_t      pwm_en_w_pin;

 /* PWM Driving signals initialization ----------------------------------------*/
   FunctionalState EmergencyStop;  /*!< It enable/disable the management of
                                        an emergency input instantaneously
                                        stopping PWM generation. It must be
                                        either equal to ENABLE or DISABLE */
} R1_VL1Params_t;

/**
  * @brief The PWMC_R1_VL1_Handle_t structure defines the handle of PWM & Current Feedback
  *        component designed for STM32F10x Medium and Low Density with 1 shunt current sensing topology.
  *
  * The design of the PWMC_R1_VL1 component is based on that of the PWMC generic component.
  */
typedef struct
{
  PWMC_Handle_t _Super;       /**< The handle on the base PWMC component. */

  uint32_t wPhaseOffset;      /**< Offset of current sensing network  */
  uint16_t Half_PWMPeriod;    /**< Half PWM Period in timer clock counts */
  uint16_t hDmaBuff[2];       /**< Buffer used for PWM distortion points*/
  uint16_t hCCDmaBuffCh4[4];  /**< Buffer used for dual ADC sampling points*/
  uint16_t hCntSmp1;          /**< First sampling point express in timer counts*/
  uint16_t hCntSmp2;          /**< Second sampling point express in timer counts*/
  uint8_t sampCur1;           /**< Current sampled in the first sampling point*/
  uint8_t sampCur2;           /**< Current sampled in the second sampling point*/
  int16_t hCurrAOld;          /**< Previous measured value of phase A current*/
  int16_t hCurrBOld;          /**< Previous measured value of phase B current*/
  int16_t hCurrCOld;          /**< Previous measured value of phase C current*/
  uint8_t bInverted_pwm;      /**< This value indicates the type of the previous PWM period.
                                *
                                *  Possible values are:
                                *  - Regular (#INVERT_NONE)
                                *  - Distort PHA (#INVERT_A)
                                *  - Distort PHB (#INVERT_B)
                                *  - distort PHC (#INVERT_C) */
  uint8_t bInverted_pwm_new;  /**< This value indicates the type of the current PWM period.
                                *
                                *  Possible values are:
                                *  - Regular (#INVERT_NONE)
                                *  - Distort PHA (#INVERT_A)
                                *  - Distort PHB (#INVERT_B)
                                *  - distort PHC (#INVERT_C) */
  uint8_t bDMATot;            /**< Value to indicate the total number of expected
                                *  DMA TC events*/
  uint8_t bDMACur;            /**< Current number of DMA TC events occurred */
  uint16_t hFlags;            /**< Internal Flags used for operation.
                                *
                                * Can take the following values:
                                * - #EOFOC: Flag to indicate end of FOC duty available
                                * - #STBD3: Flag to indicate which phase has been distorted
                                *          in boundary 3 zone (A or B)
                                * - #DSTEN: Flag to indicate if the distortion must be
                                *          performed or not (charge of bootstrap
                                *          capacitor phase)
                                * - #SOFOC: This flag will be reset to zero at the begin of FOC
                                *          and will be set in the UP IRQ. If at the end of
                                *          FOC it is set the software error must be generated
                                * - #CALIB: This flag is used to indicate the ADC calibration
                                *          phase in order to avoid concurrent regular conversions*/

  bool OverCurrentFlag;       /*!< This flag is set when an over current occurs.*/

  R1_VL1Params_t const * pParams_str; /**< Pointer on the parameters structure for the PWMC R1 VL1 component. */
  
} PWMC_R1_VL1_Handle_t;

/* Exported functions ------------------------------------------------------- */

/* Initializes a PWMC_R1_VL1 component */
void R1VL1_Init( PWMC_R1_VL1_Handle_t * pHandle );

/* Handles the Timer Interrupts of a PWMC_R1_VL1 component */
void * R1VL1_TIM1_UP_IRQHandler( PWMC_R1_VL1_Handle_t * pHandle );
void * R1VL1_DMA_TC_IRQHandler( PWMC_R1_VL1_Handle_t * pHandle );

/* Calibrates current reading for a PWMC_R1_VL1 component */
void R1VL1_CurrentReadingCalibration( PWMC_Handle_t * pHandle );

/* Returns the last phase currents values measured*/
void R1VL1_GetPhaseCurrents( PWMC_Handle_t * pHandle, ab_t* pStator_Currents );

/* Switches on PWM generation */
void R1VL1_SwitchOnPWM( PWMC_Handle_t * pHandle );

/* Switches off PWM generation */
void R1VL1_SwitchOffPWM( PWMC_Handle_t * pHandle );

/* Turns the low side switches on */
void R1VL1_TurnOnLowSides( PWMC_Handle_t * pHandle );

/* Computes the duty cycle for the next PWM period */
uint16_t R1VL1_CalcDutyCycles( PWMC_Handle_t * pHandle );

/* @brief  It contains the Break event interrupt */
void *R1VL1_BRK_IRQHandler(PWMC_R1_VL1_Handle_t *pHandle);

/* Returns whether an over current condition has occurred */
uint16_t R1VL1_IsOverCurrentOccurred( PWMC_Handle_t * pHandle );

/**
  * @}
  */
  
/**
  * @}
  */

/** @} */

#ifdef __cplusplus
}
#endif /* __cpluplus */

#endif /*__R1_VL1_PWM_CURR_FDBK_H*/
/******************* (C) COPYRIGHT 2019 STMicroelectronics *****END OF FILE****/
